<?php

namespace app\admin\controller\individuality;

use addons\shopro\service\Wallet as WalletService;
use app\common\controller\Backend;

/**
 * 个体户提现记录
 *
 * @icon fa fa-circle-o
 */
class Withdrawal extends Backend
{

    /**
     * Withdrawal模型对象
     * @var \app\admin\model\individuality\Withdrawal
     */
    protected $model = null;
    protected $is_individuality = 0;
    protected $admin = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\individuality\Withdrawal;
        $this->view->assign("statusList", $this->model->getStatusList());
        //新增lina start
        $group_ids=$this->auth->getGroupIds();
        if($group_ids[0]==2){
            //个体户
            $this->admin=$this->auth->getUserInfo();
            if(!empty($this->admin["individuality_id"])){
                $this->is_individuality=1;
            }
        }
        //新增lina end
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where_indivi=[];
            if($this->is_individuality==1){
                //个体户
                $where_indivi=["individuality.id"=>$this->admin["individuality_id"]];
            }
            $list = $this->model
                    ->with(['admin','individuality'])
                    ->where($where)
                    ->where($where_indivi)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['username']);
				$row->getRelation('individuality')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items(),"is_individuality"=>$this->is_individuality);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 审核
     * */
    public function operation($id = null)
    {
        $param = $this->request->param();
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($param["status"] == 1){
            //审核通过
            $param["remark"] = '';
        }else{
            //驳回
            WalletService::changeIndividuality($row["individuality_id"], $row["money"], [
                'order_id' => $row->id,
            ],"提现退回",1);
        }
        $param["admin_id"]=$this->auth->id;
        $result = $row->allowField(true)->save($param);
        if ($result !== false) {
            $this->success();
        } else {
            $this->error(__('No rows were updated'));
        }
    }

}
